package com.openbravo.pos.forms;

import cn.ekuma.epos.ERPContent;
import cn.ekuma.epos.datalogic.define.DataLogicSystemHelper;
import com.openbravo.bean.AppUser;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppProperties;
import com.openbravo.pos.util.Hashcypher;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Locale;
import java.util.Properties;

public class ERPRootApp extends JRootApp{

    public  void enterKeyProc() {
        AppUser user = null;
        String vcard=new String(m_txtKeys.getPassword());
        if(vcard!=null&&!vcard.isEmpty()){

            //if(!AppLocal.APP_MenuModel_Customer.equalsIgnoreCase(menuModel)){
                try {
                    user = m_dlSystem.findPeopleByCard(vcard);
                } catch (BasicException e) {
                    e.printStackTrace();
                }
           /** }else{
                try {
                    loginCustomer=m_dlCustomer.findCustomerInfoByCard(vcard, null);
                    if(loginCustomer!=null){
                        user=m_dlSystem.findPeopleById(AppConfig.CUSTOMER_MAP_GUESTID);
                        user.setM_sName(loginCustomer.getName());
                    }
                } catch (BasicException e) {
                    e.printStackTrace();
                }
            }**/
            if (user == null)  {
                // user not found
                MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.nocard"));
                msg.show(this);
            } else {
                openAppView(user);
            }
        }else{
            String number=jTextField1.getText();
            String pass=Hashcypher.hashString(new String(jPasswordField1.getPassword()));
            if(number==null||number.isEmpty()){
                MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.nouser"));
                msg.show(this);
                jTextField1.requestFocus();
            }

                try {
                    user = m_dlSystem.findPeopleByNumberAndPass(number,pass);
                } catch (BasicException e) {
                    e.printStackTrace();
                }
            }
            if (user == null)  {
                // user not found
                MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.nouser"));
                msg.show(this);
            } else {
                openAppView(user);
            }

    }

   public  boolean initApp(AppProperties props, AbstractSwingAppFactory swingAppFactory){

        m_props = props;
        // support for different component orientation languages.
        applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));

        this.swingApp=swingAppFactory.creactPosSwingApp(props,this);

        modeManager=swingApp.getManager();
        pmf=modeManager.getPluginManager();

        if(!swingApp.initApp(m_props))
            return false;

        m_dlCustomer=swingApp.getM_dlCustomer();
        swingModel=swingApp.getSwingModel();

        swingApp.start();

        m_dlSystem=swingApp.getM_dlSystem();


        // Cargamos las propiedades de base de datos
        m_propsdb = DataLogicSystemHelper.getResourceAsProperties(m_dlSystem,m_props.getHost() + "/properties");

        //合并全收局设团里
        Properties m_APPpropsdb=DataLogicSystemHelper.getResourceAsProperties(m_dlSystem,"APP_Global");
        m_props.merge(m_APPpropsdb);

        // Leo la localizacion de la caja (Almacen).
        m_sInventoryLocation = m_propsdb.getProperty("location");
        if (m_sInventoryLocation == null) {
            m_sInventoryLocation = "0";
            m_propsdb.setProperty("location", m_sInventoryLocation);
            DataLogicSystemHelper.setResourceAsProperties(m_dlSystem,m_props.getHost() + "/properties", m_propsdb);
        }

        String delayHouseText= m_propsdb.getProperty("delayHouse");
        if(delayHouseText==null)
            delayHouse=0;
        else
            delayHouse=new Integer(delayHouseText);

        try{
            defLocation=m_dlSystem.findLocation(m_sInventoryLocation);
          }catch (BasicException e) {
            // Casco. Sin caja no hay pos
            MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.cannotclosecash"), e);
            msg.show(this);
            return false;
        }

/**
        // Inicializo la impresora...
        m_TP = new DeviceTicket(pmf,this,  m_props);

        // Inicializamos
        m_TTP = new TicketParser(getDeviceTicket(), m_dlSystem);
        printerStart();

        // Inicializamos la bascula
        try {
            m_Scale = new DeviceScale(pmf,this, m_props);
        } catch (ScaleException e2) {
            e2.printStackTrace();
        }

        // Inicializamos la scanpal
        try {
            m_Scanner = new DeviceScanner(pmf,this,  m_props).getScanner();
        } catch (DeviceScannerException e1) {

            e1.printStackTrace();
        }
**/
        // Leemos los recursos basicos
        BufferedImage imgicon = DataLogicSystemHelper.getResourceAsImage(m_dlSystem,"Window.Logo");
        m_jLblTitle.setIcon(imgicon == null ? null : new ImageIcon(imgicon));
        m_jLblTitle.setText(m_dlSystem.getResourceAsText("Window.Title"));

        String sWareHouse;
        try {
            sWareHouse = m_dlSystem.findLocationName(m_sInventoryLocation);
        } catch (BasicException e) {
            sWareHouse = null; // no he encontrado el almacen principal
        }

        // Show Hostname, Warehouse and URL in taskbar
        String url="";
        //try {
        //      url = session.getURL();
        //} catch (SQLException e) {
        //    url = "";
        //}
        m_jHost.setText("<html>" + m_props.getHost() + " - " + sWareHouse + "<br>" + url);

        showLogin();

        return true;
    }


    protected void showLogin() {
        jScrollPane1.setVisible(false);
        txtCommand.setVisible(false);
        loginCustomer=null;
        menuModel=System.getProperty(ERPContent.APP_MenuModelPropName, ERPContent.APP_DefaultMenuModel);
        String showPeoplePanel=m_props.getProperty("listPeople");
        if(showPeoplePanel==null)
            showPeoplePanel="true";
        if(showPeoplePanel.equalsIgnoreCase("true")){
            // Show Login
            listPeople();
        }
        showView("login");

        userState=null;

        // keyboard listener activation
        inputtext = new StringBuffer();
        m_txtKeys.setText(null);
        jTextField1.setText(null);
        jPasswordField1.setText(null);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                jTextField1.requestFocus();
            }
        });
        setMaxWindow(false);
    }

    @Override
    public void initRolePermission(AppUser appuser) {

    }
}
